//index.js
const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    tabbar: [],
    tabPage: app.globalData.tabPage,
    list: [
      {
        icon: '../../assets/images/my/my_icon_100@3x.png',
        laber: '资金流水',
        url: '/pages/My/Account/index',
        // url: '/pages/My/Account/staff-index',
        status: [0, 1, 2, 3]
      },
      {
        icon: '../../assets/images/my/staff_icon_10@2x.png',
        laber: '我的账户',
        url1: '/pages/My/Account/account',
        url2: '/pages/My/Account/staff-index',
        url: '/pages/My/Account/account',
        status: [0, 1, 2, 3, 4]
      },
      {
        icon: '../../assets/images/my/staff_icon_11@2x.png',
        laber: '我的银行卡',
        url: '/pages/My/bank-card',
        status: []
      },
      {
        icon: '../../assets/images/my/staff_icon_12@2x.png',
        laber: '提现记录',
        url: '/pages/My/Account/record',
        status: [0, 1, 2, 3]
      },
      {
        icon: '../../assets/images/my/staff_icon_13@2x.png',
        laber: '在线客服',
        url: 'contact',
        status: [0, 1, 2, 3, 4]
      },
      {
        icon: '../../assets/images/my/staff_icon_14@2x.png',
        laber: '意见反馈',
        url: '/pages/My/feedback',
        status: [0, 1, 2, 3]
      },
      // {
      //   icon: '../../assets/images/my/staff_icon_15@2x.png',
      //   laber: '代理商招商细则',
      //   url: '/pages/Login/agreement?id=15&title=代理商招商细则',
      //   status: [0, 1, 2, 3]
      // }
    ],
    services: [],
    agentIndexData: {},
    infoPath: ''
  },
  onLoad(option) {
    this.setData({
      tabbar: app.globalData.tabbar,
    })
  },
  onShow() {
    var that = this;
    var agentIndexData = app.globalData.agentIndexData;
    var list = that.data.list;
    var services = [];
    const sessionId = wx.getStorageSync('token');
    console.log('onshow--agentIndexData==', agentIndexData);
    
    app.judgeAgent(type => {
      console.log('judgeAgent--type==', type);
      if (type === 4) {
        list[1].url = `/pages/My/Account/staff-index?id=${agentIndexData.id}`;
        agentIndexData = wx.getStorageSync('staffIndex');
        agentIndexData.type = 4;
        that.data.infoPath = `/pages/My/info?id=${agentIndexData.id}`
      } else {
        list[1].url = `/pages/My/Account/account?id=${agentIndexData.id}`;
        agentIndexData = wx.getStorageSync('agentIndex');
        that.data.infoPath = `/pages/Index/Agent/detail?id=${agentIndexData.id}&title=个人信息`;
      }
    })
    services = list.filter(d => {
      return d.status.includes(agentIndexData.type);
    })
    console.log('agentIndexData==', agentIndexData);
    that.setData({
      agentIndexData,
      sessionId,
      services: services,
      infoPath: that.data.infoPath
    })
  },
  logout() {
    var that = this;
    wx.removeStorageSync('token');
    wx.removeStorageSync('agentIndex');
    wx.removeStorageSync('agentLogin');
    wx.removeStorageSync('tabPage');
    wx.showToast({
      title: '已退出登录',
      icon: 'none'
    });
    that.setData({
      sessionId: ''
    });
    setTimeout(() => {
      app.openPage({url: '/pages/Login/login'});
    }, 1000);
  },
  goInfo() {
    var that = this;
    var url = '';
    if ( 1==1 ) {
      url = '/pages/My/info?id=1';
    }
    app.openPage({url});
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },
  tabChange(e) {
    app.tabChange(e);
  }
})